package com.example.hw_smart_home.huawei_api.auth;

import android.util.Log;

import com.example.hw_smart_home.huawei_api.pojo.auth.*;
import com.example.hw_smart_home.huawei_api.util.Constants;
import com.example.hw_smart_home.huawei_api.util.HttpUtils;
import com.example.hw_smart_home.huawei_api.util.JsonUtils;
import java.io.*;
import java.util.*;

public class Authentication {

/*    public static void main(String[] args) throws  IOException {
        System.out.println(getToken());
    }*/

    public static String getToken() throws IOException {
        /*File file = new File("token.text");

        String token = "";
        String str = "";
        if (file.exists()) {
            BufferedReader bufferedReader = null;
            InputStreamReader inputStreamReader = null;
            try {
                inputStreamReader = new InputStreamReader(new FileInputStream(file));
                bufferedReader = new BufferedReader(inputStreamReader);
                while ((str = bufferedReader.readLine()) != null) {
                    token = token + str;
                }
            } catch (IOException e) {
                System.out.println("token exist.");
            } finally {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    System.out.println("close stream faild");
                }
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    System.out.println("close stream faild");
                }
            }
        }

        if (token.trim().length() > 0) {
            return token;
        }*/
        // 构建认证信息对象
        AccessTokenDTO accessTokenDTO = new AccessTokenDTO();
        AuthDTO authDTO = new AuthDTO();
        DomainDTO domainDTO = new DomainDTO();
        IdentityDTO identityDTO = new IdentityDTO();
        PasswordDTO passwordDTO = new PasswordDTO();
        UserDTO userDTO = new UserDTO();
        ProjectDTO projectDTO = new ProjectDTO();
        ScopeDTO scopeDTO = new ScopeDTO();
        // 填写认证信息
        projectDTO.setName("cn-north-4");
        scopeDTO.setProject(projectDTO);
        domainDTO.setName("lt2580081803");
        userDTO.setName("lt");
        userDTO.setPassword("2580081803lT.");
        userDTO.setDomain(domainDTO);
        passwordDTO.setUser(userDTO);

        identityDTO.setPassword(passwordDTO);
        identityDTO.setMethods(Collections.singletonList("password"));
        authDTO.setIdentity(identityDTO);
        authDTO.setScope(scopeDTO);
        accessTokenDTO.setAuth(authDTO);

        HttpUtils httpUtils = new HttpUtils();

        Map<String, String> header = new HashMap<String, String>();
        header.put("Content-Type", "application/json");

        // 发送请求
        HttpUtils.ResponseWithHeaders response = httpUtils.doPostWithHeaders(
                Constants.TOKEN_ACCESS_URL,
                header,
                JsonUtils.Obj2String(accessTokenDTO)
        );

        if (response == null || response.headers == null) {
            System.err.println("网络请求失败或未获取到响应头");
            return null;
        }

        // 从响应头中获取 X-Subject-Token
        String token = response.headers.get("X-Subject-Token");
        if (token == null) {
            System.err.println("未获取到 X-Subject-Token");
            return null;
        }

        Log.d("token", "token: " + token);

        return token;
    }
}
